Public Class frmMain

    Private Sub btnChooseDir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChooseDir.Click

        Dim folderDialog As New FolderBrowserDialog
        folderDialog.RootFolder = Environment.SpecialFolder.Desktop
        If (folderDialog.ShowDialog() = DialogResult.OK) Then
            Me.txtDirectory.Text = folderDialog.SelectedPath
            LoadFiles(Me.txtDirectory.Text.ToString, ListView1)
        Else
        End If

    End Sub

    Sub LoadFiles(ByVal path As String, ByRef objlistview As ListView)
        Dim files() As String
        objlistview.Items.Clear()
        If Dir(path, FileAttribute.Directory) <> "" Then
            files = IO.Directory.GetFiles(path)
            For Each f As String In files
                objlistview.Items.Add(f)
            Next
        End If
    End Sub

    Private Sub BtnModifyFileName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnModifyFileName.Click
        Dim newfilename As String = ""
        Dim oldfilename As String = ""
        Dim delC As String = ""
        Dim sing As Boolean = False
        Dim counts As Integer = 0
        delC = txtCharacter.Text.ToString
        If delC = "" Then
            MsgBox("还没设置要剔除文件名中的字符呢", MsgBoxStyle.Information)
            txtCharacter.Focus()
            Exit Sub
        End If
        If ListView1.Items.Count <= 0 Then Exit Sub
        For i As Integer = 0 To ListView1.Items.Count - 1
            oldfilename = ListView1.Items(i).Text.ToString
            If oldfilename.Contains(delC) Then
                sing = True
                counts = counts + 1
                newfilename = oldfilename.Replace(delC, "")
                FileSystem.Rename(oldfilename, newfilename)
            End If
        Next
        If sing Then
            LoadFiles(Me.txtDirectory.Text.ToString, ListView1)
            MsgBox("" & counts & "个文件名中的字符 " & delC & " 清除完毕！")
            txtCharacter.SelectAll()
        End If
    End Sub

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListView1.View() = View.List
        Label3.ForeColor = Color.Red

    End Sub

    Private Sub txtCharacter_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCharacter.KeyPress
        Static invalidateCharactor As String = "<>/\?""|:*"
        If invalidateCharactor.IndexOf(e.KeyChar) >= 0 Then
            e.Handled = True
            Label3.Text = "文件名中不得含有" & invalidateCharactor & "其中的任何一个字符!"
        Else
            e.Handled = False
            Label3.Text = ""
        End If

    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Application.Exit()
    End Sub
End Class
